﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace QUAN_LY_MUON_TRA_SACH
{
    public class ConSQL
    {
        private string DataSource = null;
        public string dataSource
        {
            get { return DataSource; }
            set { DataSource = value; }
        }
        //Chuoi ket noi
        private string connectionString = "";
        public string ConnectionString
        {
            get { return connectionString; }
            set { connectionString = value; }
        }
        //Dac quyen ket noi
        private bool windowsNT = true;
        public bool WindowsNT
        {
            get { return windowsNT; }
            set { windowsNT = value; }
        }
        //Ten may chu ket noi
        private string serverName =null;
        public string ServerName
        {
            get { return serverName; }
            set { serverName = value; }
        }
        //Ten the hien cua HQTCSDL
        private string instanceName = "";
        public string InstanceName
        {
            get { return instanceName; }
            set { instanceName = value; }
        }
        //Ten CSDL
        private string databaseName = "master";
        public string DatabaseName
        {
            get { return databaseName; }
            set { databaseName = value; }
        }
        //Username dang nhap
        private string userName = "sa";
        public string UserName
        {
            get { return userName; }
            set { userName = value; }
        }
        //Mat khau
        private string passWord = "";
        public string PassWord
        {
            get { return passWord; }
            set { passWord = value; }
        }
        //Phan biet hoa thuong tren tung thuoc tinh chuoi ket noi
        private bool persistSecurityInfo = false;
        public bool PersistSecurityInfo
        {
            get { return persistSecurityInfo; }
            set { persistSecurityInfo = value; }
        }
        //Thoi gian cho ket noi
        private string connectionTimeout = "";
        public string ConnectionTimeout
        {
            get { return connectionTimeout; }
            set { connectionTimeout = value; }
        }
        //Cong ket noi
        private string portNo = "";
        public string PortNo
        {
            get { return portNo; }
            set { portNo = value; }
        }
        //Cac ham xay dung
        public ConSQL() { }
        public ConSQL(string connectionString)
        {
            this.connectionString = connectionString;
        }
        public ConSQL(string serverName, string databaseName)
        {
            this.serverName = serverName;
            this.databaseName = databaseName;
        }
        public ConSQL(string serverName, string databaseName, string userName, string passWord)
        {
            this.serverName = serverName;
            this.databaseName = databaseName;
            this.userName = userName;
            this.passWord = passWord;
            this.windowsNT = false;
        }
        //Phuong thuc tao chuoi ket noi tu cac thuoc tinh
        public string GetConnectionString()
        {
            string conString = "";
            if (!instanceName.Equals(""))
            {
                serverName += @"\" + instanceName;
            }
            if (windowsNT)
            {
                conString = "server=" + serverName + ";database=" + databaseName + ";Integrated Security=true";
            }
            else
            {
                conString = "server=" + serverName + ";database=" + databaseName + ";uid=" + userName + ";pwd=" + passWord;
            }
            if (persistSecurityInfo)
            {
                conString += ";Persist Security Info=" + persistSecurityInfo;
            }
            if (!connectionTimeout.Equals(""))
            {
                conString += ";Connection Timeout=" + connectionTimeout;
            }
            if (!portNo.Equals(""))
            {
                conString += ";Port=" + portNo;
            }
            return conString;
        }
       
    }
    public class Connection
    {
        public static SqlConnection sqlConnection;
        static Connection()
        {
            sqlConnection = new SqlConnection();
        }
        private static string strError = "";
        public static string StrError
        {
            get { return Connection.strError; }
        }
        private static string connectionString = "";
        public static string ConnectionString
        {
            get { return Connection.connectionString; }
            set { Connection.connectionString = value; }
        }
        //Phuong ket noi
        public static bool Connect()
        {
            bool Connected = false;
            try
            {
                sqlConnection.ConnectionString = connectionString;
                sqlConnection.Open();
                Connected = true;
            }
            catch (Exception ex)
            {
                strError = "Error: " + ex.Message;
            }
            return Connected;
        }
        //Phuong thuc tao ket noi lai
        public static bool reConnect()
        {
            bool Connected = false;
            if (sqlConnection.State == ConnectionState.Open)
            {
                sqlConnection.Close();
            }
            try
            {
                sqlConnection.ConnectionString = connectionString;
                sqlConnection.Open();
                Connected = true;
            }
            catch (Exception ex)
            {
                strError = "Error: " + ex.Message;
            }
            return Connected;
        }
        //Phuong thuc dong ket noi
        public static bool Close()
        {
            bool Closed = false;
            if (sqlConnection.State != ConnectionState.Closed)
            {
                sqlConnection.Close();
                sqlConnection.Dispose();
                Closed=true;
            }
            return Closed;
        }
    }
}
